//
//  CTOfficialMessageCell.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/10.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTOfficialMessageCell.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTRequestUtil.h"
#import "CTMessageItems.h"
#import "CTAppLanguage.h"
#import "CTTextManager.h"

@interface CTOfficialMessageCell ()
{
    UIImageView * headImageView;
    UIImageView * VipImageView;
    UIImageView * isNotReadImageView;
    
    UILabel * vipDescribeLabel;
    UILabel * nameLabel;
    UILabel * describeLabel;
    UILabel * timeLabel;
    
    UIView * bottomLine;
}

@end



@implementation CTOfficialMessageCell

- (void)dealloc
{
    [bottomLine removeFromSuperview];
    bottomLine = nil;
    
    [headImageView removeFromSuperview];
    headImageView = nil;
    
    [nameLabel removeFromSuperview];
    nameLabel = nil;
    
    [describeLabel removeFromSuperview];
    describeLabel = nil;
    
    [timeLabel removeFromSuperview];
    timeLabel = nil;
    
    [VipImageView removeFromSuperview];
    VipImageView = nil;
    
    [vipDescribeLabel removeFromSuperview];
    vipDescribeLabel = nil;
    
    [isNotReadImageView removeFromSuperview];
    isNotReadImageView = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTOfficialMessageCell Is Removed!!!"];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        
        [self creatSubViews];
    }
    return self;
}

-(void)creatSubViews
{
    headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 45, 45)];
    headImageView.layer.cornerRadius = 22.5;
    headImageView.layer.masksToBounds = YES;
    headImageView.image = [UIImage imageNamed:@"head"];
    [self addSubview:headImageView];
    
    isNotReadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 10, 10)];
    isNotReadImageView.image = [UIImage imageNamed:@"weidutongzhi"];
    isNotReadImageView.hidden = YES;
    [self addSubview:isNotReadImageView];
    
    VipImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip"]];
    [self addSubview:VipImageView];
    
    vipDescribeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    vipDescribeLabel.font = [UIFont systemFontOfSize:12.0];
    vipDescribeLabel.textColor = CT_HEADER_DEEPLINE_COLOR;
    [self addSubview:vipDescribeLabel];
    
    nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [nameLabel setNumberOfLines:0];
    nameLabel.lineBreakMode = NSLineBreakByWordWrapping;
    nameLabel.font = [UIFont systemFontOfSize:15.0];
    nameLabel.textColor = CT_HEADER_DEEPLINE_COLOR;
    [self addSubview:nameLabel];
    
    describeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [describeLabel setNumberOfLines:0];
    describeLabel.lineBreakMode = NSLineBreakByWordWrapping;
    describeLabel.font = [UIFont systemFontOfSize:16.0];
    describeLabel.textColor = [UIColor blackColor];
    [self addSubview:describeLabel];
    
    timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    timeLabel.font = [UIFont systemFontOfSize:12.0];
    timeLabel.textColor = [UIColor colorWithRed:203/255.0 green:203/255.0 blue:199/255.0 alpha:1.0];
    [self addSubview:timeLabel];
    
    bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 0.3)];
    bottomLine.backgroundColor = [UIColor colorWithRed:201/255.0 green:201/255.0 blue:201/255.0 alpha:1.0];
    [self addSubview:bottomLine];
}



#pragma mark - setter -

-(void)setOfficialMessageItem:(CTOfficialMessageItem *)item
{
   
    [CTRequestUtil requestImgWithUrl:item.extras.send_avatar withCompletionSuccessHandler:^(UIImage *img) {
        headImageView.image = img;
    } withFailedHandler:^(NSError *error) {
        
    }];
    
    
    CGSize labelSize;
    labelSize = [item.extras.send_name sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:CGSizeMake(100, 15) lineBreakMode:NSLineBreakByWordWrapping];
    nameLabel.frame = CGRectMake(65,20,labelSize.width, labelSize.height);
    nameLabel.text = item.extras.send_name;
    
    VipImageView.frame = CGRectMake(nameLabel.frame.origin.x + nameLabel.bounds.size.width + 5, 22.5, 13, 13);
    
    vipDescribeLabel.frame = CGRectMake(nameLabel.frame.origin.x + nameLabel.bounds.size.width+5 + 13 + 4, 23, 100, 12.0);
    vipDescribeLabel.text = [CTAppLanguage localizedStringForKey:@"message_describelabel" withValue:nil withTable:@"Language"];
    
    
    CGSize descriLabelSize;
    descriLabelSize = [item.content sizeWithFont:[UIFont systemFontOfSize:16.0] constrainedToSize:CGSizeMake((CT_HEADER_SCREEN_WIDTH - 60-65-15), 1000) lineBreakMode:NSLineBreakByWordWrapping];
    describeLabel.frame = CGRectMake(65,45,descriLabelSize.width, descriLabelSize.height);
    describeLabel.text = item.content;
    
    
    timeLabel.frame = CGRectMake(65, describeLabel.frame.origin.y + describeLabel.bounds.size.height +12, 150, 12);
    timeLabel.text = [[CTTextManager shareInstance] timestampToTime:item.extras.send_time];
    
    
    
    self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, timeLabel.frame.origin.y + timeLabel.bounds.size.height +15);
    
    bottomLine.frame = CGRectMake(0,self.bounds.size.height - 0.5, CT_HEADER_SCREEN_WIDTH, 0.5);
    
    if ([item.isRead isEqualToString:@"0"])isNotReadImageView.hidden = NO;
    else isNotReadImageView.hidden = YES;
}





@end